import React from "react";
import withRetry from "@/compnents/ErrorBoundary/withRetry";
// import AppMain from "@/views/AppMain";
// import AppProList from "@/views/AppProManager/AppProList";
// import AppFilterList from "@/views/AppProManager/AppFilterList";
// import AppUserList from "@/views/AppUserManger/AppUserList";
// import AppAdminLIst from "@/views/AppUserManger/AppAdminLIst";
import { cloneDeep } from "lodash";
const AppMain = withRetry(() => import("@/views/AppMain"));
const AppProList = withRetry(() => import("@/views/AppProManager/AppProList"));
const AppFilterList = withRetry(() =>
  import("@/views/AppProManager/AppFilterList")
);
const AppUserList = withRetry(() =>
  import("@/views/AppUserManger/AppUserList")
);
const AppAdminLIst = withRetry(() =>
  import("@/views/AppUserManger/AppAdminLIst")
);
export const menus = [
  {
    key: "/",
    label: "首页",
    component: AppMain,
    exact: true,
  },
  {
    label: "商品管理",
    key: "/pro",
    children: [
      {
        label: "商品列表",
        key: "/pro-list",
        component: AppProList,
      },
      {
        label: "商品筛选",
        key: "/pro-filter-list",
        component: AppFilterList,
      },
    ],
  }, // 菜单项务必填写 key
  {
    label: "用户与管理员",
    key: "/user-admin",
    children: [
      {
        label: "用户列表",
        key: "/user-list",
        component: AppUserList,
      },
      {
        label: "管理员列表",
        key: "/admin-list",
        component: AppAdminLIst,
      },
    ],
  },
];
// 推平二维数组
export const filterMenusToRoutes = (items) => {
  let result = [];
  items.forEach((item) => {
    if (!item.children) result.push({ ...item });
    else result = result.concat(filterMenusToRoutes(item.children));
  });
  return result;
};
//根据当前的路由路径取得menu中的一级key
export const getMenuKeyByPathname = (pathname, items) => {
  console.log("传递的pathnmae", pathname);
  const result = [];
  for (let i = 0; i < items.length; i++) {
    if (items[i].children) {
      for (let j = 0; j < items[i].children.length; j++) {
        const element = items[i].children[j];
        if (element.key === pathname) {
          result.push(items[i].key);
          break;
        }
      }
    }
  }
  return result;
};
//根据权限过滤菜单
export const filterMenusByAuthority = (items, checkedKeys) => {
  const results = [];
  items.forEach((item) => {
    // 如果有1级 就代表内部的2级都有权限
    if (checkedKeys.includes(item.key)) {
      results.push(item);
    } else {
      // 如果没有一级 需要判断一下这个一级内部有每一某一个二级有权限
      const newItem = { ...cloneDeep(item), children: [] };
      item.children?.forEach((v) => {
        if (checkedKeys.includes(v.key)) newItem.children.push(v);
      });
      if (newItem.children.length) {
        results.push(newItem);
      }
    }
  });
  return results;
};
export default menus;
